﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Globalization;

namespace ReportGenerator
{
    public class XmlReportGenerator
    {
        public static void Main(string[] args)
        {
            WriteXml();
        }

        public static void WriteXml()
        {
            string fileName = "../../report.xml";
            Encoding encoding = Encoding.GetEncoding("windows-1251");
            using (XmlTextWriter writer = new XmlTextWriter(fileName, encoding))
            {
                writer.Formatting = Formatting.Indented;
                writer.IndentChar = '\t';
                writer.Indentation = 1;
                writer.WriteStartDocument();
                WriteHeader(writer);
                WriteSales(writer);
            }
            Console.WriteLine("Document {0} created.", fileName);
        }

        public static void WriteSales(XmlWriter writer)
        {
            BookstoreDataContext context = new BookstoreDataContext();
            DateTime firstDayOfCurrentMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            DateTime nextMonth = DateTime.Now.AddMonths(1);
            DateTime firstDayOfNextMonth = new DateTime(DateTime.Now.Year, nextMonth.Month, 1);
            using (context)
            {
                List<Book> books =
                      (from b in context.Books
                       select b).ToList();
                decimal? totalIncomes = 0M;
                foreach (Book currentbook in books)
                {
                    writer.WriteStartElement("book-sale");
                    List<Sale> sales =
                        (from s in context.Sales
                         where s.BookID == currentbook.BookID
                            && s.Date > firstDayOfCurrentMonth
                            && s.Date < firstDayOfNextMonth
                         select s).ToList();

                    string isbn = currentbook.ISBN;
                    int amount = 0;
                    foreach (Sale currentSale in sales)
                    {
                        amount += currentSale.Amount;
                    }
                    decimal? incomes = amount * currentbook.Price;
                    totalIncomes += incomes;
                    writer.WriteElementString("isbn", isbn.ToString());
                    writer.WriteElementString("amount", amount.ToString());
                    writer.WriteElementString("incomes", incomes.ToString());
                    writer.WriteEndElement();
                }
                writer.WriteElementString("total-incomes", totalIncomes.ToString());
            }
        }

        public static void WriteHeader(XmlWriter writer)
        {
            writer.WriteStartElement("sales-report");

            CultureInfo culture = new CultureInfo("en-GB");
            DateTime now = DateTime.Now;
            DateTime firstDayOfCurrentMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            DateTime nextMonth = DateTime.Now.AddMonths(1);
            DateTime firstDayOfNextMonth = new DateTime(DateTime.Now.Year, nextMonth.Month, 1);
            DateTime lastDayOfCurrentMonth = firstDayOfNextMonth.AddDays(-1);
            string firstDay = firstDayOfCurrentMonth.ToString(culture);
            string endDay = lastDayOfCurrentMonth.ToString(culture);

            writer.WriteAttributeString("start-date", firstDay);
            writer.WriteAttributeString("end-date", endDay);
        }
    }
}
